package com.xmm.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import com.xmm.constants.CONSTANTS;
import com.xmm.listener.MenuActionListener;

public class MusicTreeNodeMenu extends JPopupMenu {
	/**
	 * 
	 */
	private static final long serialVersionUID = 5260615630752626879L;
	private int mType;
	
	private MusicTreeNode mNode = null;
	private MenuActionListener mMenuActionListener = null;
	
	public MusicTreeNodeMenu(MusicTreeNode node) {
		super();
		
		mNode = node;
		init(node.getType());
	}
	
	public void init(int type) {
		mType = type;
		
		if(type == CONSTANTS.NODE_TYPE_ROOT) {
			JMenuItem addlib = new JMenuItem(CONSTANTS.MUSIC_TREE_MENU_ADD_LIBRARY);
			addlib.addActionListener(addlibListener);
			add(addlib);
		} else if(type == CONSTANTS.NODE_TYPE_LIBRARY) {
			JMenuItem addablum = new JMenuItem(CONSTANTS.MUSIC_TREE_MENU_ADD_ABLUM);
			JMenuItem addtrack = new JMenuItem(CONSTANTS.MUSIC_TREE_MENU_ADD_TRACK);
			JMenuItem addtrackbypeople = new JMenuItem(CONSTANTS.MUSIC_TREE_MENU_ADD_TRACK_BY_PEOPLE);
			JMenuItem dellib = new JMenuItem(CONSTANTS.MUSIC_TREE_MUNU_DEL_LIBRARY);
			addablum.addActionListener(addablumListener);
			addtrack.addActionListener(addtrackListener);
			addtrackbypeople.addActionListener(addtrackbypeopleListener);
			dellib.addActionListener(dellibListener);
			add(addablum);
			add(addtrack);
			add(addtrackbypeople);
			add(dellib);
		} else if(type == CONSTANTS.NODE_TYPE_ABLUM) {
			JMenuItem addtrack = new JMenuItem(CONSTANTS.MUSIC_TREE_MENU_ADD_TRACK);
			JMenuItem addtrackbypeople = new JMenuItem(CONSTANTS.MUSIC_TREE_MENU_ADD_TRACK_BY_PEOPLE);
			JMenuItem delablum = new JMenuItem(CONSTANTS.MUSIC_TREE_MUNU_DEL_ABLUM);
			addtrack.addActionListener(addtrackListener);
			addtrackbypeople.addActionListener(addtrackbypeopleListener);
			delablum.addActionListener(delablumListener);
			add(addtrack);
			add(addtrackbypeople);
			add(delablum);
		} else if(type == CONSTANTS.NODE_TYPE_TRACK) {
			JMenuItem deltrack = new JMenuItem(CONSTANTS.MUSIC_TREE_MUNU_DEL_TRACK);
			deltrack.addActionListener(deltrackListener);
			add(deltrack);
		}
	}
	
	public void setMenuActionListener(MenuActionListener menuActionListener) {
		mMenuActionListener = menuActionListener;
	}
	
	public int getType() {
		return mType;
	}
	
	private ActionListener addlibListener = new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent e) {
			mMenuActionListener.doAddLib(mNode);
		}
	};
	
	private ActionListener addablumListener = new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent e) {
			mMenuActionListener.doAddAblum(mNode);
		}
	};
	
	private ActionListener addtrackListener = new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent e) {
			mMenuActionListener.doAddTrack(mNode);
		}
	};
	
	private ActionListener addtrackbypeopleListener = new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent e) {
			mMenuActionListener.doAddTrackByPeople(mNode);
		}
	};
	
	private ActionListener dellibListener = new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent e) {
			mMenuActionListener.doDelLib(mNode);
		}
	};
	
	private ActionListener delablumListener = new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent e) {
			mMenuActionListener.doDelAblum(mNode);
		}
	};
	
	private ActionListener deltrackListener = new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent e) {
			mMenuActionListener.doDelTrack(mNode);
		}
	};
}
